/**
 * @TextTipView.as
 *
 * @author sodaChen mail:sujun10#21cn.com
 * @version 1.0
 * <br>Copyright (C), 2014 FeiYin.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:Battle
 * <br>Date:2014-9-30
 */
package com.asframe.display.tip
{
	import com.asframe.utils.HtmlTextUtils;

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	import org.bytearray.display.ScaleBitmap;

	/**
	 * 文本提示显示对象
	 * @author sodaChen
	 * Date:2014-9-30
	 */
	public class TextTipView extends Sprite implements ITipView
	{
		public static var offsetX:int = 15;
		public static var offsetY:int = 15;
		/** 是否自动缩放显示尺寸 **/
		private var autoSize:Boolean = true;
		/** 文本的默认颜色 **/
		public var textColor:uint = 0x000000;
		public var textSize:uint = 12;
		/** 显示的宽度 **/
		public var showWidth:int = 100;
		public var showHeight:int = 100;

		protected var textField:TextField;
		protected var background:DisplayObject;
		/** 会被缩放的9宫格背景 **/
		protected var scaleBitmap:ScaleBitmap;
	//	protected var scale9Grid:Rectangle;

		public function TextTipView()
		{
			mouseChildren = false;
			textField = new TextField();
			addChild(textField);
			textField.mouseEnabled = false;
			textField.selectable = false;
			textField.multiline = true;
			textField.wordWrap = false;
			textField.autoSize = TextFieldAutoSize.LEFT;

		}
		public function isAutoSize(autoSize:Boolean):void
		{
			this.autoSize = autoSize;
			if(!autoSize)
			{
				textField.width = showWidth;
				textField.height = showHeight;
			}
		}
		public function addDisplayObject(displayObject:DisplayObject):void
		{
			addChild(displayObject);
			addChild(textField);
		}
		/**
		 * 设置背景
		 * @param background 背景对象或者url
		 * @param scale9Grid 如果是九宫格方式，则必须传该值
		 *
		 */
		public function setBackground(background:*,scale9Grid:Rectangle = null):void
		{
			this.scale9Grid = scale9Grid;
			var bitmapData:BitmapData = null;
			if(background is String)
			{
				return ;
			}
			else if(background is DisplayObject || background is BitmapData)
			{
				initBackground(background);
			}
			else
			{
				throw new Error("必须是字符串URL或者显示对象");
			}
		}
		private function initBackground(background:*):void
		{
			var bitmapData:BitmapData = null;
			if(background is Bitmap)
			{
				bitmapData = Bitmap(bitmapData).bitmapData;
			}
			else if(background is BitmapData)
			{
				bitmapData = BitmapData(background);
			}
			if(bitmapData != null)
			{
				if(scale9Grid != null)
				{
					scaleBitmap = new ScaleBitmap(bitmapData);
					background = scaleBitmap;
				}
				else
				{
					background = new Bitmap(background);
				}
			}
			if(scale9Grid != null)
			{
				background.scale9Grid = scale9Grid;
				//调整文本的位置
				textField.x = scale9Grid.x;
				textField.y = scale9Grid.y;
			}
			this.background = background;
			addChildAt(background,0);
		}
		public function setTipData(data:*):void
		{
			var str:String = null;
			if(data is String)
				str = String(data);
			else
				str =data.toString();
			if(str.indexOf("<") == -1)
				str = HtmlTextUtils.getColorString(str,"#" + textColor.toString(16),false,textSize);
			textField.htmlText = str;
			//重绘一下背景
			drawBackground();
		}
		protected function drawBackground():void
		{
			if(background != null)
			{
				background.width = textField.width;
				background.height = textField.height;
				if(scale9Grid != null)
				{
					background.width += scale9Grid.x * 2;
					background.height += scale9Grid.y * 2;
				}
			}
		}
		public function getContainer():Sprite
		{
			return this;
		}

		public function getWidth():int
		{
			if(autoSize)
			{
				if(width < textField.width)
					return textField.width;
				return width;
			}
			return showWidth;
		}

		public function getHeight():int
		{
			if(autoSize)
			{
				if(height < textField.height)
					return textField.height;
				return height;
			}
			return showHeight;
		}
	}
}